package net.tooeye.easymes.activities;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;


import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;

import net.tooeye.easymes.common.BaseActivity;
import net.tooeye.easyplus.R;
import net.tooeye.easymes.common.NetworkUtil;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Console;
import java.util.HashMap;
import java.util.Map;

public class SfctaActivity extends BaseActivity implements View.OnClickListener,TextView.OnEditorActionListener,TextView.OnFocusChangeListener {

    private Button buttonPost;
    private EditText editTextBar,editTextCount,editTextWeight, editTextTech,editTextEquipment;
    private TextView textView_sfcta,textView_cmsmx;
    private RadioButton  radioButton,radioButton3;
    private  TextView moctata001,textVewBarcode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sfcta);
        hideActionBar();
        init();
    }

    private void init() {
        Intent intent = getIntent();
        buttonPost = findViewById(R.id.buttonPost);
        buttonPost.setOnClickListener(this);
        editTextBar = findViewById(R.id.editbar);
        String bar;
        try{
            bar = intent.getStringExtra("bar");
        }
        catch (Exception ex ){
            bar = "";
        }
        editTextBar.setText(bar);
        editTextBar.setOnEditorActionListener(this);
        editTextBar.setOnFocusChangeListener(this);
        editTextCount = findViewById(R.id.editCount);
        editTextCount.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        editTextTech = findViewById(R.id.editTech);
        editTextEquipment = findViewById(R.id.editEquipment);
        editTextWeight = findViewById(R.id.editWeight);
        editTextWeight.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        SharedPreferences sp = getSharedPreferences("easy_plus", Context.MODE_PRIVATE);
        editTextEquipment.setText(sp.getString("EquipmentNo",""));
        editTextTech.setText(sp.getString("TechniqueNo",""));
        radioButton = findViewById(R.id.radioButton2);
        radioButton3 = findViewById(R.id.radioButton3);
        textView_sfcta = findViewById(R.id.textView_sfcta);
        textView_cmsmx = findViewById(R.id.textView_cmsmx);
        textView_sfcta.setOnClickListener(new OpenSfctaSelect());
        textView_cmsmx.setOnClickListener(new OpenCmsmxSelect());

        moctata001 = findViewById(R.id.moctata001);

        textVewBarcode = findViewById(R.id.textVewBarcode);
        textVewBarcode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new IntentIntegrator(SfctaActivity.this)
                        .setPrompt("请对准一维或二维码")
                        .setCaptureActivity(ScanActivity.class)
                        .setCameraId(0)
                        .setBeepEnabled(true)
                        .initiateScan();
            }
        });

    }

    private Map<String,String>  GetMap(){
        Map<String,String> params = new HashMap<>();
        params.put("token",GetToken());
        params.put("barcode",editTextBar.getText().toString());
        params.put("Count",editTextCount.getText().toString());
        params.put("Weight",editTextWeight.getText().toString());
        params.put("ManHours","0");
        params.put("TechniqueNo", editTextTech.getText().toString());
        params.put("EquipmentNo",editTextEquipment.getText().toString());
        String status = "1";  //正常加工
        if (radioButton.isChecked()){
            status = "2";   //产品返工
        }
        if (radioButton3.isChecked()){
            status = "3";   //产品报废
        }
        params.put("Status",status);
        return  params;
    }

    @Override
    public void onClick(View v) {
        if (!checkInput()){
            return;
        }
        Map<String,String> params  = GetMap();
        String url = "http://" + GetHost() + "/Barcode/AddSfcBarcode";
        @SuppressLint("HandlerLeak") Handler handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                JSONObject jsonObject;
                try {
                    jsonObject = new JSONObject((String)msg.obj);
                    Log.d("song", "handleMessage: "+ jsonObject.getString("Message"));
                    if (jsonObject.getBoolean("Status")) {
                        SharedPreferences sp = getSharedPreferences("easy_plus", Context.MODE_PRIVATE);
                        SharedPreferences.Editor editor = sp.edit();
                        editor.putString("TechniqueNo", editTextTech.getText().toString());
                        editor.putString("EquipmentNo", editTextEquipment.getText().toString());
                        editor.apply();
                        Toast.makeText(SfctaActivity.this, "提交成功", Toast.LENGTH_LONG).show();
                        finish();//或者清空
                    }else{
                        Toast.makeText(SfctaActivity.this, "提交失败,"+jsonObject.getString("Message")
                                , Toast.LENGTH_LONG).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        };
        NetworkUtil.getInstance().post(SfctaActivity.this,url,params,handler,1,GetToken());
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
       //工艺
        if ( requestCode == 1){
            try{
                editTextTech.setText(data.getStringExtra("sfcta"));
            }
            catch (Exception e){
                Log.e("easyplus", "onActivityResult: "+ e.toString());
            }
            return;
        }
        //设备
        if(requestCode == 2){
            try{
                editTextEquipment.setText(data.getStringExtra("cmsmx"));
            }catch (Exception e){
                Log.e("easyplus", "onActivityResult: "+ e.toString());
            }
            return;
        }
       //摄像头

        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if(result != null) {
            if(result.getContents() != null) {
                editTextBar.setText(result.getContents());
            }
        }
    }
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        moctata001.setText("这里需要查询网络，暂时没有");
        return false;
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {


    }


    private  boolean checkInput(){
        if(editTextBar.getText().length() < 1){
            Toast.makeText(SfctaActivity.this, "条形码不能为空", Toast.LENGTH_LONG).show();
            return  false;
        }
        if(editTextCount.getText().length() < 1){
            Toast.makeText(SfctaActivity.this, "数量不能为空", Toast.LENGTH_LONG).show();
            return  false;
        }
        return  true;
    }

    class  OpenSfctaSelect implements  View.OnClickListener {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(SfctaActivity
                    .this,SfctaSelectActivity.class);
            startActivityForResult(intent,1);
        }
    }

    class  OpenCmsmxSelect implements  View.OnClickListener {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(SfctaActivity
                    .this,CmsmxSelectActivity.class);
            startActivityForResult(intent,2);
        }
    }

}
